package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BEArticulo;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DAArticulo extends DAOAbstract<BEArticulo> {

    public DAArticulo(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BEArticulo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_articulo");

            setParameterString(be.getColor());
            setParameterString(be.getMaterial());
            setParameterString(be.getUbicacion());
            setParameterString(be.getDescripcion());
            setParameterBinaryStream(be.getImagen());
            setParameterDouble(be.getPreciocompra());
            setParameterDouble(be.getPrecioventa());
            setParameterInt(be.getMarcaid());
            setParameterInt(be.getTipoid());
            setParameterString(be.getExtension());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEArticulo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_articulo");
            setParameterInt(be.getArticuloid());
            setParameterString(be.getColor());
            setParameterString(be.getMaterial());
            setParameterString(be.getUbicacion());
            setParameterString(be.getDescripcion());
            setParameterBinaryStream(be.getImagen());
            setParameterDouble(be.getPreciocompra());
            setParameterDouble(be.getPrecioventa());
            setParameterInt(be.getMarcaid());
            setParameterInt(be.getTipoid());
            setParameterString(be.getExtension());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEArticulo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_articulo");
            setParameterInt(be.getArticuloid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEArticulo> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_articulo");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEArticulo be = new BEArticulo();
                be.setArticuloid(getDataInt(1));
                be.setColor(getDataString(2));
                be.setMaterial(getDataString(3));
                be.setUbicacion(getDataString(4));
                be.setDescripcion(getDataString(5));

                be.setPreciocompra(getDataDouble(6));
                be.setPrecioventa(getDataDouble(7));
                be.setMarcaid(getDataInt(8));
                be.setTipoid(getDataInt(9));
                be.setMarca(getDataString(10));
                be.setTipo(getDataString(11));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEArticulo> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_articulo");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEArticulo be = new BEArticulo();
                be.setArticuloid(getDataInt(1));
                be.setColor(getDataString(2));
                be.setMaterial(getDataString(3));
                be.setUbicacion(getDataString(4));
                be.setDescripcion(getDataString(5));
                be.setPreciocompra(getDataDouble(6));
                be.setPrecioventa(getDataDouble(7));
                be.setMarcaid(getDataInt(8));
                be.setTipoid(getDataInt(9));
                be.setMarca(getDataString(10));
                be.setTipo(getDataString(11));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEArticulo buscar(int i) throws Exception {
        try {
            BEArticulo be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_articulo_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEArticulo();
                be.setArticuloid(getDataInt(1));
                be.setColor(getDataString(2));
                be.setMaterial(getDataString(3));
                be.setUbicacion(getDataString(4));
                be.setDescripcion(getDataString(5));
                be.setImagen(getDataBinaryStream(6, "default." + getDataString(11)));
                be.setPreciocompra(getDataDouble(7));
                be.setPrecioventa(getDataDouble(8));
                be.setMarcaid(getDataInt(9));
                be.setTipoid(getDataInt(10));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public int obtenerUltimoId() throws Exception {
        BEArticulo be = new BEArticulo();
        try {
            setStoreProcedure("sp_obtenerarticuloid");
            while (existResult()) {
                be.setArticuloid(getDataInt(1));
            }

            return Integer.parseInt(commit().toString());

        } catch (Exception e) {
            return rollback(e);
        }
    }

}
